
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ycbl/config/bl_color.dart';
import 'package:ycbl/config/bl_style.dart';
import 'package:ycbl/widget/image.dart';


class BLAppBar extends StatelessWidget implements PreferredSizeWidget{

  /// 标题
  final Widget title;

  /// 左侧按钮
  final Widget left;

  /// 右侧按钮
  final List<Widget> rightActions;

  /// 标题是否居中
  final bool isTitleCenter;

  /// 背景色
  final Color backgroundColor;

  /// 状态栏文字是否为白色
  final bool isWhiteStatusBar;

  /// 通常配合 SliverAppBar 使用
  final Widget flexibleSpace;

  /// 覆盖在 AppBar 之上的 Widget
  final Widget childOverAppBar;

  // 继承自 PreferredSizeWidget
  @override
  final Size preferredSize;

  const BLAppBar({
    Key key,
    this.title,
    this.left,
    this.rightActions,
    this.isTitleCenter,
    this.backgroundColor,
    this.isWhiteStatusBar,
    this.flexibleSpace,
    this.childOverAppBar,
  }) :  preferredSize = const Size.fromHeight(kToolbarHeight),
        super(key: key);

  @override
  Widget build(BuildContext context) {


    return Stack(
        children: [
          AppBar(
            key: key,
            title: title,
            leading: left,
            actions: rightActions,
            centerTitle: isTitleCenter,
            backgroundColor: backgroundColor,
            brightness: isWhiteStatusBar ? Brightness.dark : Brightness.light,
            flexibleSpace: flexibleSpace,
            elevation: 0,
          ),
          childOverAppBar ?? Container(width: 0, height: 0,)
        ],
    );
  }

}


/// 通用的白色 AppBar
class BLCommonWhiteAppBar extends BLAppBar {

  /// 标题文字
  final String titleText;
  /// 左侧按钮
  final Widget leftIcon;
  /// 左侧按钮点击
  final VoidCallback onLeftPressed;
  /// 右侧按钮
  final List<Widget> rightActions;
  /// 标题是否居中
  final bool isTitleCenter;
  /// 是否隐藏左侧按钮
  final bool hideLeftIcon;

  BLCommonWhiteAppBar({
    Key key,
    BuildContext context,
    this.titleText,
    this.leftIcon,
    this.onLeftPressed,
    this.rightActions,
    this.isTitleCenter = true,
    this.hideLeftIcon = false,
  }): super(
    title: Text(titleText ?? '', style: BLStyle.largeBlackTextStyle,),
    left: hideLeftIcon ? null : FlatButton(
        onPressed: onLeftPressed ?? (){
          if(context != null && Navigator.of(context).canPop()) {
            //默认返回
            Navigator.of(context).pop();
          }
        },
        child: leftIcon ?? LocalImage('ic_back_grey.png',)
    ),
    rightActions: rightActions,
    backgroundColor: BLColor.whiteColor,
    isWhiteStatusBar: false,
    isTitleCenter: isTitleCenter,
  );

}

/// 通用的橘色 AppBar
class BLCommonOrangeAppBar extends BLAppBar {

  /// 标题文字
  final String titleText;
  /// 左侧按钮
  final Widget leftIcon;
  /// 左侧按钮点击
  final VoidCallback onLeftPressed;
  /// 右侧按钮
  final List<Widget> rightActions;
  /// 标题是否居中
  final bool isTitleCenter;
  /// 是否隐藏左侧按钮
  final bool hideLeftIcon;
  /// 是否隐藏左侧按钮
  final bool isWhiteStatusBar;

  BLCommonOrangeAppBar({
    Key key,
    BuildContext context,
    this.titleText,
    this.leftIcon,
    this.onLeftPressed,
    this.rightActions,
    this.isTitleCenter = true,
    this.hideLeftIcon = false,
    this.isWhiteStatusBar = false,
  }): super(
    title: Text(titleText ?? '', style: BLStyle.largeWhiteTextStyle,),
    left: hideLeftIcon ? null : FlatButton(
        onPressed: onLeftPressed ?? (){
          if(context != null && Navigator.of(context).canPop()) {
            //默认返回
            Navigator.of(context).pop();
          }
        },
        child: leftIcon ?? LocalImage('ic_back_white.png')
    ),
    rightActions: rightActions,
    backgroundColor: Color(0xFFFF5301),
    isWhiteStatusBar:isWhiteStatusBar,
    isTitleCenter: isTitleCenter,
    childOverAppBar: Positioned(
      right: 40,
      bottom: 0,
      child: LocalImage('pic_paopao.png',  height: 35,),
    ),
  );

}

class BLCustomAppbar extends StatefulWidget implements PreferredSizeWidget {
  final double contentHeight; //从外部指定高度
  final Color navigationBarBackgroundColor; //设置导航栏背景的颜色
  final Widget leadingWidget;
  final Widget trailingWidget;
  final String title;
  BLCustomAppbar({
    @required this.leadingWidget,
    @required this.title,
    this.contentHeight = 44,
    this.navigationBarBackgroundColor = Colors.white,
    this.trailingWidget,
  }) : super();

  @override
  State<StatefulWidget> createState() {
    return new _BLCustomAppbarState();
  }

  @override
  Size get preferredSize => new Size.fromHeight(contentHeight);
}

/// 这里没有直接用SafeArea，而是用Container包装了一层
/// 因为直接用SafeArea，会把顶部的statusBar区域留出空白
/// 外层Container会填充SafeArea，指定外层Container背景色也会覆盖原来SafeArea的颜色
///     var statusheight = MediaQuery.of(context).padding.top;  获取状态栏高度

class _BLCustomAppbarState extends State<BLCustomAppbar> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      color: widget.navigationBarBackgroundColor,
      child: new SafeArea(
        top: true,
        child: new Container(
            decoration: new UnderlineTabIndicator(
              borderSide: BorderSide(width: 1.0, color: Color(0xFFeeeeee)),
            ),
            height: widget.contentHeight,
            child: new Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Positioned(
                  left: 0,
                  child: new Container(
                    padding: const EdgeInsets.only(left: 5),
                    child: widget.leadingWidget,
                  ),
                ),
                new Container(
                  child: new Text(widget.title,
                      style: new TextStyle(
                          fontSize: 17, color: Color(0xFF333333))),
                ),
                Positioned(
                  right: 0,
                  child: new Container(
                    padding: const EdgeInsets.only(right: 5),
                    child: widget.trailingWidget,
                  ),
                ),
              ],
            )),
      ),
    );
  }
}




class BLHomeAppbar extends StatefulWidget implements PreferredSizeWidget {
  final double contentHeight; //从外部指定高度
  Color navigationBarBackgroundColor; //设置导航栏背景的颜色
  final double elevation;
  Widget leadingWidget;
  Widget trailingWidget;
  final Color backgroundColor;
  String title;
  BLHomeAppbar({
    @required this.leadingWidget,
    @required this.title,
    this.backgroundColor = Colors.deepOrange,
    this.elevation = 0,
    this.contentHeight = 44,
    this.navigationBarBackgroundColor = Colors.white,
    this.trailingWidget,
  }) : super();

  @override
  State<StatefulWidget> createState() {
    return new _BLHomeAppbarState();
  }

  @override
  Size get preferredSize => new Size.fromHeight(contentHeight);
}

/// 这里没有直接用SafeArea，而是用Container包装了一层
/// 因为直接用SafeArea，会把顶部的statusBar区域留出空白
/// 外层Container会填充SafeArea，指定外层Container背景色也会覆盖原来SafeArea的颜色
///     var statusheight = MediaQuery.of(context).padding.top;  获取状态栏高度

class _BLHomeAppbarState extends State<BLHomeAppbar> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
  return Semantics(
    container: true,
    child: AnnotatedRegion<SystemUiOverlayStyle>(
      ///修改状态栏颜色
      value: SystemUiOverlayStyle.dark,
      child: Material(
        color: widget.backgroundColor,
//        backgroundColor:Colors.deepOrange,
        elevation: widget.elevation,
//        shape: widget.shape,
        child: Semantics(
          explicitChildNodes: true,
          child: Container(
            color: widget.navigationBarBackgroundColor,
            child: new SafeArea(
              top: true,
              child: new Container(
                  height: widget.contentHeight,
                  child: new Stack(
                    alignment: Alignment.center,
                    children: <Widget>[
                      Positioned(
                        left: 0,
                        child: new Container(
                          padding: const EdgeInsets.only(left: 5),
                          child: widget.leadingWidget,
                        ),
                      ),
                      new Container(
                        child: new Text(widget.title,
                            style: new TextStyle(
                                fontSize: 17, color: Color(0xFF333333))),
                      ),
                      Positioned(
                        right: 0,
                        child: new Container(
                          padding: const EdgeInsets.only(right: 16),
                          child: widget.trailingWidget,
                        ),
                      ),
                    ],
                  )),
            ),
          ),
        ),
      ),
    ),
  );
  }
}